home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / math / matrx042 / matrix.doc < prev    next >
Encoding:
Text File  |  1994-04-15  |  17.9 KB  |  483 lines

  1. /*
  2. *-----------------------------------------------------------------------------
  3. *       file:   matrix.doc
  4. *       desc:   document for matrix toolbox function calls
  5. *       by:     KO shu pui, patrick
  6. *       date:   24 may 92       v0.4
  7. *               23 sep 93       v0.41
  8. *               16 apr 94       v0.42
  9. *-----------------------------------------------------------------------------
  10. */
  11.  
  12. ===============================================================================
  13. 0. INTRODUCTION
  14. ===============================================================================
  15. This document only provides you the following information:
  16.  
  17.         0.1     How to create and free a matrix
  18.         0.2     Description of each matrix function call
  19.         0.3     Some hints to use this toolbox
  20.  
  21. Remember that this document will NOT describe the data structure and
  22. any technical details of the toolbox - just because this document is
  23. aimed at to be a sort-of "User's Guide" for programmers.
  24.  
  25.  
  26.  
  27.  
  28. ===============================================================================
  29. 1. OUR MATRIX OF REFERENCE
  30. ===============================================================================
  31. In order to avoid terms confusion, here is our matrix of reference
  32. (matrix A) which is of size m x n.
  33.  
  34.                           Column
  35.                 Row        0       1       2          n-1
  36.                  0    [   a0,0    a0,1    a0,2   ...  a0,n-1   ]
  37.      A =         1    [   a1,0    a1,1    a1,2   ...  a1,n-1   ]
  38.                  2    [   a2,0    a2,1    a2,2   ...  a2,n-1   ]
  39.                       [   ...     ...     ...    ...  ...      ]
  40.                  m-1  [   am-1,0  am-1,1  am-1,2 ...  am-1,n-1 ]
  41.  
  42.  
  43.  
  44. ===============================================================================
  45. 2. BASIC MATRIX OBJECT OPERATION
  46. ===============================================================================
  47. -------------------------------------------------------------------------------
  48. Function :      MATRIX mat_creat (int m, int n, int type)
  49. Synopsis :      creation of a matrix which can be used by the matrix toolbox
  50.                 functions; memory is allocated for this object; and some
  51.                 initialization is performed.
  52. Parameter:      m: number of rows
  53.                 n: number of columns
  54.                 type: matrix initialization type where
  55.  
  56.                 type=
  57.  
  58.                 UNDEFINED       do not initialize the matrix
  59.                 ZERO_MATRIX     fill zero to all matrix elements
  60.                 UNIT_MATRIX     fill a one to all matrix element ai,j
  61.                                 where i=j
  62.  
  63. Return Value:   the matrix object
  64. Example:
  65.  
  66.                 MATRIX  A;
  67.  
  68.                 /*
  69.                 * create a 15 x 15 matrix;
  70.                 * do not initialize the elements
  71.                 */
  72.                 A = mat_creat( 15, 15, UNDEFINED);
  73.  
  74.                 /*
  75.                 * put a 4 in element A(0,14) of matrix A,
  76.                 * put a 2 in element A(3,5) of matrix A
  77.                 */
  78.                 A[0][14] = 4.0;
  79.                 A[3][5] = 2.0;
  80.  
  81. See Also:       mat_free(), for Accessing a matrix, see this example.
  82. -------------------------------------------------------------------------------
  83. Function:       MATRIX mat_fill ( MATRIX A, int type )
  84. Synopsis:       initialize a matrix will a simple type
  85. Parameter:      A: the matrix object for which mat_creat() has been called
  86.                 type: matrix initialization type where
  87.  
  88.                 type=
  89.  
  90.                 UNDEFINED       do not initialize the matrix
  91.                 ZERO_MATRIX     fill zero to all matrix elements
  92.                 UNIT_MATRIX     fill a one to all matrix element ai,j
  93.                                 where i=j
  94. Return Value:   MATRIX A
  95. Example:
  96.  
  97.                 MATRIX  A;
  98.  
  99.                 ...
  100.                 /*
  101.                 * fill the matrix A will zeroes
  102.                 */
  103.                 mat_fill( A, ZERO_MATRIX );
  104.  
  105. See Also:       mat_creat()
  106. -------------------------------------------------------------------------------
  107. Function :      int mat_free ( MATRIX A )
  108. Synopsis :      free all memory occupied by the matrix object A
  109. Parameter:      A: the matrix object for which mat_creat() was called
  110. Return Value:   None
  111. Example:
  112.  
  113.                 MATRIX  A;
  114.  
  115.                 A = mat_creat(...);
  116.                 ...
  117.                 mat_free(A);
  118.  
  119. Note:           since memory may be a very scarce resource in a computer,
  120.                 as a C programmer you are advised to free up the matrix as
  121.                 soon as that matrix will not be used any more in future.
  122.  
  123. See Also:       mat_creat()
  124. -------------------------------------------------------------------------------
  125. Function:       MatCol ( A )
  126. Synopsis:       find out the number of columns of a matrix object A
  127. Parameter:      A: the matrix object for which mat_creat() was called
  128. Return Value:   number of columns
  129. Example:
  130.                 int     i;
  131.  
  132.                 ...
  133.                 i = MatCol(A);
  134.  
  135. Note:           this is a macro
  136. See Also:       MatRow()
  137. -------------------------------------------------------------------------------
  138. Function:       MatRow ( A )
  139. Synopsis:       find out the number of rows of a matrix object A
  140. Parameter:      A: the matrix object for which mat_creat() was called
  141. Return Value:   number of rows
  142. Example:
  143.                 int     i;
  144.  
  145.                 ...
  146.                 i = MatRow(A);
  147.  
  148. Note:           this is a macro
  149. See Also:       MatCol()
  150.  
  151. -------------------------------------------------------------------------------
  152. Function:       MATRIX mat_colcopy1 ( MATRIX A, MATRIX B, int j1, int j2 )
  153. Synopsis:       copy a column from a matrix A to a column at matrix B
  154. Parameter:      A, B: the matrix objects for which mat_creat() was called
  155.                 column j1 of A is copied to column j2 of B;
  156. Return Value:   MATRIX A
  157. Example:
  158.                 MATRIX  A, B;
  159.  
  160.                 A = mat_creat( 5, 4, ZERO_MATRIX );
  161.                 B = mat_creat( 5, 4, UNDEFINED );
  162.  
  163.                 /*
  164.                 * copy column 2 of A to column 0 of B
  165.                 */
  166.                 mat_colcopy1( A, 2, B, 0 );
  167.  
  168. Note:           the sizes of rows of A, B must be the same
  169. See Also:       mat_copy()
  170. -------------------------------------------------------------------------------
  171. Function:       MATRIX mat_copy ( MATRIX A )
  172. Synopsis:       duplicate a matrix
  173. Parameter:      A: the matrix object for which mat_creat() was called
  174. Return Value:   Another allocated matrix object whose contents are same
  175.                 as A
  176. Example:
  177.                 MATRIX  A, B;
  178.  
  179.                 A = mat_creat( 5, 4, ZERO_MATRIX );
  180.  
  181.                 /*
  182.                 * B is also a 5 x 4 zero matrix now
  183.                 */
  184.                 B = mat_copy( A );
  185.                 ...
  186.                 mat_free( B );
  187.  
  188. See Also:       mat_colcopy1()
  189. -------------------------------------------------------------------------------
  190.  
  191.  
  192.  
  193.  
  194. ===============================================================================
  195. 3. BASIC MATRIX OBJECT INPUT/OUTPUT OPERATION
  196. ===============================================================================
  197. -------------------------------------------------------------------------------
  198. Function:       int fgetmat (MATRIX A, FILE * fp)
  199. Synopsis:       read a matrix from stream
  200. Parameter:      A: allocated matrix
  201.                 fp: a stream pointer obtained from fopen() or predefined
  202.                 pointer in standard library such as stdin
  203. Return Value:   number of matrix elements read
  204. Example:
  205.                 MATRIX  A;
  206.                 FILE    *fp;
  207.  
  208.                 A = mat_creat(3, 3, UNDEFINED);
  209.                 fp = fopen("mymatrix.dat", "r");
  210.                 fgetmat( A, fp );
  211.  
  212. See Also:       mat_dumpf(), mat_dump(), mat_fdump(), mat_fdumpf()
  213. -------------------------------------------------------------------------------
  214. Function:       MATRIX mat_dump   (MATRIX A)
  215.                 MATRIX mat_dumpf  (MATRIX A, char * format)
  216.                 MATRIX mat_fdump  (MATRIX A, FILE * fp)
  217.                 MATRIX mat_fdumpf (MATRIX A, char * format, FILE * fp)
  218.  
  219. Synopsis:       mat_dump:  dump a matrix to std output with default format
  220.                 mat_dumpf: dump a matrix to std output with specified format
  221.                 mat_fdump: dump a matrix to a file with default format
  222.                 mat_fdumpf:dump a matrix to a file with specified format
  223.  
  224. Parameter:      A: allocated matrix
  225.                 format: same as printf() 's format parameter, but can only
  226.                         be floating point type, such as "%.2f ", etc.
  227.                 fp: file pointer obtained via fopen()
  228.  
  229. Return Value:   matrix A
  230.  
  231. Example:
  232.                 MATRIX  A;
  233.  
  234.                 A = mat_creat( ... );
  235.                 ...
  236.                 /*
  237.                 * dump the matrix to standard output and each element
  238.                 * is restricted to 2 precision format
  239.                 */
  240.                 mat_dumpf( A, "%.2f ");
  241.  
  242. See Also:       fgetmat()
  243. -------------------------------------------------------------------------------
  244.  
  245.  
  246.  
  247.  
  248. ===============================================================================
  249. 4. BASIC MATRIX OBJECT MATHEMATICAL OPERATION
  250. ===============================================================================
  251. -------------------------------------------------------------------------------
  252. Function:       MATRIX mat_add (MATRIX A, MATRIX B);
  253.                 MATRIX mat_sub (MATRIX A, MATRIX B);
  254. Synopsis:       mat_add: addition of 2 matrices
  255.                 mat_sub: substraction of 2 matrices
  256. Parameter:      A, B: allocated matrices
  257. Return Value:   a newly allocated matrix which is the result of the operation
  258. Example:
  259.  
  260.                 MATRIX  A, B, C;
  261.  
  262.                 A = mat_creat( 5, 5, UNIT_MATRIX );
  263.                 B = mat_creat( 5, 5, UNIT_MATRIX );
  264.  
  265.                 C = mat_add( A, B );
  266.  
  267.                 mat_dump( C );
  268.  
  269.                 mat_free( A );
  270.                 mat_free( B );
  271.                 mat_free( C );
  272.  
  273. Note:           A, B must be of the same dimensions
  274. See Also:       mat_mul, mat_inv
  275. -------------------------------------------------------------------------------
  276. Function:       MATRIX mat_mul (MATRIX A, MATRIX B);
  277. Synopsis:       multiplication of 2 matrices
  278. Parameter:      A, B: allocated matrix
  279. Return Value:   a newly allocated matrix which is the result of the operation
  280. Example:
  281.                 MATRIX  A, B, C;
  282.  
  283.                 A = mat_creat( 5, 3, UNIT_MATRIX );
  284.                 B = mat_creat( 3, 6, UNIT_MATRIX );
  285.  
  286.                 /*
  287.                 *  C is now a 5 x 6 matrix
  288.                 */
  289.                 C = mat_add( A, B );
  290.  
  291.                 mat_dump( C );
  292.                 ...
  293.  
  294. Note:           the column dimension of A must equal row dimension of B
  295. See Also:       mat_add, mat_sub
  296. -------------------------------------------------------------------------------
  297. Function:       MATRIX mat_inv (MATRIX A)
  298. Synopsis:       find the inverse of a matrix
  299. Parameter:      A: allocated square matrix
  300. Return Value:   a newly allocated square matrix which is the inverse of A
  301. Example:
  302.                 MATRIX  A, AI;
  303.  
  304.                 /*
  305.                 * A must be a square matrix
  306.                 */
  307.                 A = mat_creat( 7, 7, UNIT_MATRIX );
  308.                 ...
  309.                 /*
  310.                 * find the inverse of A
  311.                 */
  312.                 AI = mat_inv( A );
  313.                 ...
  314.  
  315. See Also:       mat_tran, mat_add, mat_sub
  316. -------------------------------------------------------------------------------
  317. Function:       MATRIX  mat_tran( MATRIX A )
  318. Synopsis:       find the transpose of a matrix
  319. Parameter:      A: allocated matrix
  320. Return Value:   a newly allocated matrix which is the transpose of A
  321. Example:
  322.                 MATRIX  A, T;
  323.  
  324.                 A = mat_creat( 3, 5, UNDEFINED );
  325.                 ...
  326.                 T = mat_tran( A );
  327.                 ...
  328.  
  329. See Also:       mat_inv
  330. -------------------------------------------------------------------------------
  331.  
  332.  
  333.  
  334.  
  335. ===============================================================================
  336. 5. DETERMINANT OPERATIONS
  337. ===============================================================================
  338. -------------------------------------------------------------------------------
  339. Function:       MATRIX mat_submat (MATRIX A, int i, int j)
  340. Synopsis:       form a new matrix by deleting a row and a column of A
  341. Parameter:      A: allocated matrix
  342.                 i, j: row and column of A which will not appear in the
  343.                       resulting matrix
  344. Return Value:   a new matrix whose dimensions are 1 less than A
  345. Example:
  346.                 MATRIX  A, B
  347.  
  348.                 A = mat_creat( 3, 4, UNDEFINED );
  349.                 ...
  350.                 B = mat_submat( A, 2, 2 );
  351.                 /*
  352.                 *       suppose A =  [ 1 2 3 4 ]
  353.                 *                    [ 3 3 4 5 ]
  354.                 *                    [ 6 7 9 9 ]
  355.                 *
  356.                 *       then B =     [ 1 2 4 ]
  357.                 *                    [ 3 3 5 ]
  358.                 */
  359.  
  360. Note:           Do not be misled -- the contents in A will NOT be changed
  361. See Also:       mat_det, mat_cofact, mat_minor
  362. -------------------------------------------------------------------------------
  363. Function:       double mat_cofact (MATRIX A, int i, int j)
  364. Synopsis:       find out the cofactor of A(i,j)
  365. Parameter:      A: allocated square matrix
  366.                 i,j: row, column position of A for the cofactor
  367. Return Value:   the value of cofactor of A(i,j)
  368. Example:
  369.                 MATRIX  A;
  370.  
  371.                 A = mat_creat( 5, 5, UNIT_MATRIX );
  372.                 ...
  373.                 printf( "cofactor of A(0,2) = %f\n", mat_cofact( A, 0, 2 ));
  374.  
  375. See Also:       mat_det, mat_minor
  376. -------------------------------------------------------------------------------
  377. Function:       double mat_minor (MATRIX A, int i, int j)
  378. Synopsis:       find out the minor of A(i,j)
  379. Parameter:      A: allocated square matrix
  380.                 i,j: row, column position of A for the minor
  381. Return Value:   the value of minor of A(i,j)
  382. Example:
  383.                 MATRIX  A;
  384.  
  385.                 A = mat_creat( 5, 5, UNIT_MATRIX );
  386.                 ...
  387.                 printf( "minor of A(0,2) = %f\n", mat_minor( A, 0, 2 ));
  388.  
  389. See Also:       mat_det, mat_cofact
  390. -------------------------------------------------------------------------------
  391. Function:       double mat_det (MATRIX A)
  392. Synopsis:       find the determinant of a matrix
  393. Parameter:      A: allocated square matrix
  394. Return Value:   the determinant value of |A|
  395. Example:
  396.                 MATRIX  A;
  397.                 double  det;
  398.  
  399.                 A = mat_creat( 5, 5, UNIT_MATRIX );
  400.                 det = mat_det( A );
  401.  
  402. See Also:       mat_cofact, mat_minor, mat_submat
  403. -------------------------------------------------------------------------------
  404.  
  405.  
  406.  
  407.  
  408. ===============================================================================
  409. 6. ADVANCED MATRIX OBJECT MATHEMATICAL OPERATION
  410. ===============================================================================
  411. -------------------------------------------------------------------------------
  412. Function:       MATRIX mat_lsolve (MATRIX A, MATRIX B)
  413. Synopsis:       solve simultaneous linear equation AX = B given A, B
  414. Parameter:      A, B: allocated matrix
  415. Return Value:   newly allocated matrix X in AX = B
  416. Example:
  417.                 MATRIX  A, B, X;
  418.  
  419.                 A = mat_creat( 5, 5, UNDEFINED );
  420.                 fgetmat( A, stdin );
  421.                 ...
  422.                 B = mat_creat( 5, 1, UNDEFINED );
  423.                 fgetmat( B, stdin );
  424.                 ...
  425.                 X = mat_lsolve( A, B );
  426.                 mat_dump( X );
  427.  
  428. Note:           number of rows in A and B must be equal
  429. See Also:       mat_lsolve_durbin
  430. -------------------------------------------------------------------------------
  431. Function:       MATRIX mat_lsolve_durbin (MATRIX A, MATRIX B)
  432. Synopsis:       simultaneous linear equations wtih Levinson-Durbin method
  433. Parameter:      A, B: allocated matrix where A is an autocorrelated matrix and
  434.                       B is derived from A
  435.  
  436.                 A, B must be the following forms:
  437.  
  438.                 |  a0   a1   a2  .. an-1 | |  x1   |    |  a1   |
  439.                 |  a1   a0   a1  .. an-2 | |  x2   |    |  a2   |
  440.                 |  a2   a1   a0  .. an-3 | |  x3   |  = |  ..   |
  441.                 |  ...                   | |  ..   |    |  ..   |
  442.                 |  an-1 an-2 ..  .. a0   | |  xn   |    |  an   |
  443.  
  444.                 where A is a symmetric Toeplitz matrix and B
  445.                 in the above format (related to A)
  446.  
  447. Return Value:   a newly allocated matrix X which is the solution of AX = B
  448.  
  449. Example:        this method only applies to certain A, B only.
  450.                 e.g.
  451.  
  452.                 A =  [1 3 9]        B = [3]
  453.                      [3 1 3]            [9]
  454.                      [9 3 1]            [12]  <- the last one is unrelated to A
  455.  
  456. See Also:       mat_SymToeplz
  457. -------------------------------------------------------------------------------
  458. Function:       MATRIX mat_SymToeplz (MATRIX R);
  459. Synopsis:       create a symmetric Toeplitz matrix from a
  460.                 Autocorrelation vector
  461. Parameter:      R: allocated matrix of dimension n x 1
  462. Return Value:   a newly allocated symmetrical Toeplitz matrix
  463. Example:
  464.                 e.g.
  465.  
  466.                 MATRIX  A, R;
  467.  
  468.                 R = mat_creat( 3, 1, UNDEFINED );
  469.                 ...
  470.                 A = mat_SymToeplz( R );
  471.  
  472.                 /*
  473.                 *  if
  474.                 *
  475.                 *  R =  [1 3 9]         A =  [1 3 9]
  476.                 *                            [3 1 3]
  477.                 *                            [9 3 1]
  478.                 *
  479.                 */
  480.  
  481. See Also:       mat_lsolve_durbin
  482. -------------------------------------------------------------------------------
  483.